home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / ICON_8 / COMMON_F / TIME.C < prev   
C/C++ Source or Header  |  1990-03-11  |  7KB  |  337 lines

  1. #include "::h:config.h"
  2.  
  3. /*
  4.  * The following code is operating-system dependent [@time.01].  Include files
  5.  *  that are system-dependent.
  6.  */
  7.  
  8. #if PORT
  9.    /* probably needs something */
  10. Deliberate Syntax Error
  11. #endif                    /* PORT */
  12.  
  13. #if AMIGA
  14. #include "time.h"
  15. #endif                    /* AMIGA */
  16.  
  17. #if ATARI_ST
  18.    /* nothing is needed */
  19. #endif                    /* ATARI_ST */
  20.  
  21. #if HIGHC_386 || MVS || VM
  22. #include <time.h>
  23. #endif                    /* HIGHC_386 || MVS || ... */
  24.  
  25. #if MACINTOSH
  26. #if LSC
  27. #include <time.h>
  28. #endif                    /* LSC */
  29. #if MPW
  30. #include <types.h>
  31. #include "time.h"
  32. #include <OSUtils.h>
  33. #include <Events.h>
  34. #endif                    /* MPW */
  35. #endif                    /* MACINTOSH */
  36.  
  37. #if MSDOS
  38. #include <time.h>
  39. #if MICROSOFT
  40. #include <sys/types.h>
  41. #endif                    /* MICROSOFT */
  42. #endif                    /* MSDOS */
  43.  
  44. #if OS2
  45. #include <time.h>
  46. #include <sys/types.h>
  47. #endif                    /* OS2 */
  48.  
  49. #if UNIX
  50. #include <sys/types.h>
  51. #include <sys/times.h>
  52. #include SysTime
  53. #endif                    /* UNIX */
  54.  
  55. #if VMS
  56. #include <types.h>
  57. #include <time.h>
  58. struct tms {
  59.     time_t    tms_utime;    /* user time */
  60.     time_t    tms_stime;    /* system time */
  61.     time_t    tms_cutime;    /* user time, children */
  62.     time_t    tms_cstime;    /* system time, children */
  63. };
  64. #endif                    /* VMS */
  65.  
  66. /*
  67.  * End of operating-system specific code.
  68.  */
  69.  
  70. static char *day[] = {
  71.    "Sunday", "Monday", "Tuesday", "Wednesday",
  72.    "Thursday", "Friday", "Saturday"
  73.    };
  74.  
  75. static char *month[] = {
  76.    "January", "February", "March", "April", "May", "June",
  77.    "July", "August", "September", "October", "November", "December"
  78.    };
  79.  
  80.  
  81. /*
  82.  * getitime - fill in a "struct cal_time" with information about the current
  83.  *  time and date.
  84.  */
  85. novalue getitime(ct)
  86. struct cal_time *ct;
  87.    {
  88.  
  89. /*
  90.  * The following code is operating-system dependent [@time.02]. Declarations
  91.  *  for getting time.
  92.  */
  93.  
  94. #if PORT
  95.    long time();
  96.    long xclock;
  97. Deliberate Syntax Error
  98. #endif                    /* PORT */
  99.  
  100. #if AMIGA || OS2 || UNIX || VMS
  101.    long time();
  102.    long xclock;
  103. #endif                    /* AMIGA || OS2 || UNIX || VMS */
  104.  
  105. #if ATARI_ST
  106.    struct tm {
  107.        short tm_year;
  108.        short tm_mon;
  109.        short tm_wday;
  110.        short tm_mday;
  111.        short tm_hour;
  112.        short tm_min;
  113.        short tm_sec;
  114.    };
  115.    long xclock;
  116. #endif                    /* ATARI_ST */
  117.  
  118. #if MSDOS
  119.    long xclock;
  120. #if LATTICE || MICROSOFT || TURBO
  121.    long time();
  122. #endif                    /* LATTICE || MICROSOFT || TURBO */
  123. #if MWC
  124.    time_t time();
  125. #endif                    /* MWC */
  126. #endif                    /* MSDOS */
  127.  
  128. #if HIGHC_386
  129.    long time();
  130.    time_t xclock;
  131. #endif                    /* HIGHC_386 */
  132.  
  133. #if MACINTOSH
  134. #if LSC
  135.    unsigned long xclock;
  136.    unsigned long time();
  137. #else                    /* LSC */
  138.    time_t xclock;
  139. #endif                    /* LSC */
  140. #endif                    /* MACINTOSH */
  141.  
  142. #if MVS || VM
  143.    time_t xclock;
  144. #endif                    /* MVS || VM */
  145.  
  146. /*
  147.  * End of operating-system specific code.
  148.  */
  149.  
  150.    struct tm *tbuf, *localtime();
  151. /*
  152.  * The following code is operating-system dependent [@time.03]. Code for
  153.  *  getting time.
  154.  */
  155.  
  156. #if PORT
  157.    time(&xclock);
  158.    tbuf = localtime(&xclock);
  159. Deliberate Syntax Error
  160. #endif                    /* PORT */
  161.  
  162. #if AMIGA || HIGHC_386 || MACINTOSH || MSDOS || OS2 || UNIX || VMS || MVS || VM
  163.    time(&xclock);
  164.    tbuf = localtime(&xclock);
  165. #endif                    /* AMIGA || HIGHC || ... */
  166.  
  167. #if ATARI_ST
  168.     tbuf = localtime(&xclock);
  169. #endif                    /* ATARI_ST */
  170.  
  171. /*
  172.  * End of operating-system specific code.
  173.  */
  174.  
  175.    ct->year = 1900 + tbuf->tm_year;
  176.    ct->month_no = tbuf->tm_mon+1;
  177.    ct->month_nm = month[tbuf->tm_mon];
  178.    ct->mday = tbuf->tm_mday;
  179.    ct->wday = day[tbuf->tm_wday];
  180.    ct->hour = tbuf->tm_hour;
  181.    ct->minute = tbuf->tm_min;
  182.    ct->second = tbuf->tm_sec;
  183.    return;
  184.    }
  185.  
  186. /*
  187.  * getctime - fill a buffer with the "ctime" representation of the current
  188.  *  time and date. The buffer must be at least 26 characters.
  189.  */
  190. novalue getctime(sbuf)
  191. char *sbuf;
  192.    {
  193.    struct cal_time ct;
  194.  
  195.    getitime(&ct);
  196.    sprintf(sbuf, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", ct.wday, ct.month_nm,
  197.       ct.mday, ct.hour, ct.minute, ct.second, ct.year);
  198.    return;
  199.    }
  200.  
  201. /*
  202.  * millisec - returns execution time in milliseconds. Time is measured
  203.  *  from the fucntions's first call. The granularity of the time is
  204.  *  generally more than one millisecond and on some systems it my only
  205.  *  be accurate to the second.
  206.  */
  207. long millisec()
  208.    {
  209.    static int first_time = 1;
  210.  
  211. /*
  212.  * The following code is operating-system dependent [@time.04]. Declarations
  213.  *   that are system-dependent.
  214.  */
  215.  
  216. #if PORT
  217.    static long starttime;
  218.    long time();
  219. Deliberate Syntax Error
  220. #endif                    /* PORT */
  221.  
  222. #if AMIGA || ATARI_ST || OS2
  223.    static long starttime;
  224.    long time();
  225. #endif                    /* AMIGA || ATARI_ST || OS2 */
  226.  
  227. #if MSDOS
  228.    static long starttime;
  229. #if LATTICE || MICROSOFT || TURBO
  230.    long time();
  231. #endif                    /* LATTICE || MICROSOFT || TURBO */
  232. #if MWC
  233.    time_t time();
  234. #endif                    /* MWC */
  235. #endif                    /* MSDOS */
  236.  
  237. #if HIGHC_386
  238.    static time_t hc_strtime;
  239.    time_t hc_curtime;
  240.    long time();
  241. #endif                    /* HIGHC_386 */
  242.  
  243. #if MACINTOSH || MVS || VM
  244.    static long starttime;
  245. #endif                    /* MACINTOSH || MVS || VM */
  246.  
  247. #if UNIX || VMS
  248.    struct tms tp;
  249.    static long starttime;
  250. #endif                    /* UNIX || VMS */
  251.  
  252. /*
  253.  * End of operating-system specific code.
  254.  */
  255.  
  256.    if (first_time) {
  257.       first_time = 0;
  258.  
  259. /*
  260.  * The following code is operating-system dependent [@time.05].  Get start
  261.  *  time.
  262.  */
  263.  
  264. #if PORT
  265.       /* needs something */
  266. Deliberate Syntax Error
  267. #endif                    /* PORT */
  268.  
  269. #if AMIGA  || ATARI_ST || MSDOS || OS2
  270.       time(&starttime);    /* note: this obtains time in various units */
  271. #endif                    /* AMIGA || ATARI_ST || MSDOS */
  272.  
  273. #if HIGHC_386
  274.       time(&hc_strtime);
  275. #endif                    /* HIGHC_386 */
  276.  
  277. #if MACINTOSH
  278.       starttime = TickCount();    /* 60 ticks / second */
  279. #endif                    /* MACINTOSH */
  280.  
  281. #if MVS || VM
  282.       starttime = (long)clock();    /* microseconds */
  283. #endif                    /* MVS || VM */
  284.  
  285. #if UNIX || VMS
  286.       times(&tp);
  287.       starttime = tp.tms_utime;
  288. #endif                    /* UNIX || VMS */
  289.  
  290. /*
  291.  * End of operating-system specific code.
  292.  */
  293.  
  294.       return 0L;
  295.       }
  296.    else {    /* not first time */
  297. /*
  298.  * The following code is operating-system dependent [@time.06].  Get time.
  299.  */
  300.  
  301. #if PORT
  302.    /* needs something */
  303. Deliberate Syntax Error
  304. #endif                    /* PORT */
  305.  
  306. #if AMIGA || MSDOS || OS2
  307.       return 1000 * (time(NULL) - starttime);
  308. #endif                    /* AMIGA || MSDOS || OS2 */
  309.  
  310. #if ATARI_ST
  311.       return (time(NULL) - starttime) / 10;
  312. #endif                    /* ATARI_ST */
  313.  
  314. #if HIGHC_386
  315.       time(&hc_curtime);
  316.       return 1000 * (long)difftime(hc_curtime,hc_strtime);
  317. #endif                    /* HIGHC_386 */
  318.  
  319. #if MACINTOSH
  320.       return 1000 * ((extended)(TickCount() - starttime) / (extended)Hz);
  321. #endif                    /* MACINTOSH */
  322.  
  323. #if MVS || VM
  324.       return ((long)clock() - starttime) / 1000;
  325. #endif                    /* MVS || VM */
  326.  
  327. #if UNIX || VMS
  328.       times(&tp);
  329.       return 1000 * ((tp.tms_utime - starttime) / (double)Hz);
  330. #endif                    /* UNIX || VMS */
  331.  
  332. /*
  333.  * End of operating-system specific code.
  334.  */
  335.       }
  336.    }
  337.